package com.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.Map;

public class JwtUtils {

    private static final String singKen = "guojintao";
    private static long expire = 7 * 24 * 60 * 60 * 1000L;


//    生成令牌
    public static String genJwt(Map<String, Object> claims){
        String jwt = Jwts.builder()
                .addClaims(claims)   //给令牌内添加自定义内容
                .signWith(SignatureAlgorithm.HS256, singKen)   //设置令牌的加密算法和密钥
                .setExpiration(new Date(System.currentTimeMillis() + expire))      //设置令牌的有效期
                .compact();
        return jwt;
    }

//    解析令牌
    public static Claims parseJWT(String jwt){
        Claims claims = Jwts.parser()
                .setSigningKey(singKen)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }
}
